package com.model.common.ui.activity.initial;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.model.common.R;
import com.model.common.base.activity.BaseActivity;
import com.model.common.databinding.ActivitySplashBinding;
import com.model.common.ui.activity.WebActivity;

/**
 * Create by Carson on 2021/12/22.
 * SplashActivity --- 启动页
 */
public class SplashActivity extends BaseActivity<ActivitySplashBinding> {

    private Runnable mRunnable;
    private final Handler handler = new Handler();
    private SharedPreferences preferences;

    @Override
    protected ActivitySplashBinding getViewBinding() {
        return ActivitySplashBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initViewsAndEvents() {
        // 存在则打开它，否则创建新的Preferences
        preferences = getSharedPreferences("save_first_run", 0);
        boolean isNew = preferences.getBoolean("isNewApp", true);
        if (isNew) {
            showNewDialog();
        } else {
            chooseGo();
        }
    }

    /**
     * 进入主界面或引导页
     */
    private void chooseGo() {
        mRunnable = () -> {
            try {
                //如果用户不是第一次使用则直接调转到显示界面,否则调转到引导界面
                boolean isFirstRun = preferences.getBoolean("isFirstRun", true);
                if (isFirstRun) {
                    go(GuideActivity.class);
                } else {
                    //不是第一次使用进入主界面
                    Class className = Class.forName("com.carson.undergo.ui.activity.MainActivity");
                    Intent intent = new Intent(this, className);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
                finish();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        };
        handler.postDelayed(mRunnable, 2000);
    }

    /**
     * 权限说明弹框
     */
    private void showNewDialog() {
        final Dialog mDialog;
        mDialog = new Dialog(this, R.style.customDialog);
        mDialog.setContentView(R.layout.dialog_show_initial);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setCancelable(false);
        mDialog.show();
        TextView content = mDialog.findViewById(R.id.tv_content);

        String str = "请您务必审慎阅读、充分理解“用户协议”和“隐私政策”各条款，包括但不限于：" +
                "为了向您提供交易相关基本功能，我们会收集、使用必要的信息。你可阅读" +
                "《用户协议》" + "和" +
                "《隐私政策》" +
                "了解详细信息。如您同意，请点击“同意”接受我们的服务。";
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(str);
        //第一个出现的位置
        final int start = str.indexOf("《");
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                //用户服务协议点击事件
                Bundle bundle = new Bundle();
                bundle.putString("title", "用户协议");
                bundle.putInt("showType", 1);
                bundle.putString("content", "https://www.jianshu.com/u/8a9682c1725e");
                go(WebActivity.class, bundle);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                //设置文件颜色
                ds.setColor(getResources().getColor(R.color.colorMain));
                // 去掉下划线
                ds.setUnderlineText(false);
            }

        }, start, start + 6, 0);

        //最后一个出现的位置
        final int end = str.lastIndexOf("《");
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                //隐私协议点击事件
                Bundle bundle = new Bundle();
                bundle.putString("title", "隐私政策");
                bundle.putInt("showType", 1);
                bundle.putString("content", "https://gitee.com/poison_carson/carson-undergo");
                go(WebActivity.class, bundle);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                //设置文件颜色
                ds.setColor(getResources().getColor(R.color.colorMain));
                // 去掉下划线
                ds.setUnderlineText(false);
            }

        }, end, end + 6, 0);
        content.setMovementMethod(LinkMovementMethod.getInstance());
        content.setText(ssb, TextView.BufferType.SPANNABLE);
        //设置点击后的背景颜色为透明
        content.setHighlightColor(ContextCompat.getColor(this, R.color.colorTransparent));

        mDialog.findViewById(R.id.tv_cancel).setOnClickListener(v -> {
            mDialog.dismiss();
            finish();
        });
        mDialog.findViewById(R.id.tv_sure).setOnClickListener(v -> {
            mDialog.dismiss();
            //更改状态，同意下次进入软件则不再弹出弹框
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("isNewApp", false);
            editor.apply();
            chooseGo();
        });
    }

    @Override
    protected void onDestroy() {
        if (mRunnable != null && handler != null) {
            handler.removeCallbacks(mRunnable);
        }
        super.onDestroy();
    }
}
